<?php

defined('BASEPATH') or die('forbidden');

class Playerbug_Controller Extends Controller {

    private $buglist_db; //问题列表专用数据库

    function _init() {
        //check_level(50, segment(0), segment(1));
        //$this->buglist_db = $this->load->database('buglist');
        ;
    }

    function index() {
        //三维数组
        $data = array();

        $platfrom = isset($_REQUEST['platform']) ? $_REQUEST['platform'] : 'android';
        $channel = isset($_REQUEST['channel']) ? $_REQUEST['channel'] : 'xiaomi';
        $account = isset($_REQUEST['account']) ? $_REQUEST['account'] : 1;
        $roleid = isset($_REQUEST['roleid']) ? $_REQUEST['roleid'] : 1;
        $area = isset($_REQUEST['area']) ? $_REQUEST['area'] : 1;

        $sql = "select * from player_buglist where platform = '$platfrom' "
                . "and channel = '$channel' and area = $area and roleid = $roleid  order by isanswer asc, createtime desc";

        //echo " sql:$sql ";	
        $this->db->query($sql);
        while ($row = $this->db->get_array()) {
			
        	$row['question'] = base64_decode($row['question']);
        	
            $data[0][$row['id']]['question'] = $row['question'];
            $data[0][$row['id']]['answer'] = $row['answer'];
            $data[0][$row['id']]['createtime'] = $row['createtime'];
            $data[0][$row['id']]['replaytime'] = $row['replaytime'];
            $data[0][$row['id']]['isanswer'] = $row['isanswer'];

            if (1 == $row['isanswer']) { //已回答
                $data[1][$row['id']]['question'] = $row['question'];
                $data[1][$row['id']]['answer'] = $row['answer'];
                $data[1][$row['id']]['createtime'] = $row['createtime'];
                $data[1][$row['id']]['replaytime'] = $row['replaytime'];
                $data[1][$row['id']]['isanswer'] = $row['isanswer'];
            } else { //未回答
                $data[2][$row['id']]['question'] = $row['question'];
                $data[2][$row['id']]['answer'] = $row['answer'];
                $data[2][$row['id']]['createtime'] = $row['createtime'];
                $data[2][$row['id']]['replaytime'] = $row['replaytime'];
                $data[2][$row['id']]['isanswer'] = $row['isanswer'];
            }
        }

        //隐式的把数据传过去
        $this->view->assign("platform", $platfrom);
        $this->view->assign("channel", $channel);
        $this->view->assign("account", $account);
        $this->view->assign("area", $area);
        $this->view->assign("roleid", $roleid);
        $this->view->assign("mydata", $data);

        $this->view->display("buglist/playerbug/index");
    }

    function submit() {
        $platfrom = $_REQUEST['platform'];
        $channel = $_REQUEST['channel'];
        $account = $_REQUEST['account'];
        $roleid = $_REQUEST['roleid'];
        $question = trim($_REQUEST['question']);
        $question = base64_encode($question);
        
        $area = $_REQUEST['area'];
		$phone = $_REQUEST['phone'];
		$mail = $_REQUEST['mail'];
		
        //如果提交的内容 为空，直接返回
        if ($question == "" || $question == NULL) {
            return;
        }

        $value = array('platform' => $platfrom, 'channel' => $channel, 'accountid' => $account, 'area' => $area, 'roleid' => $roleid,
            'question' => $question, 'phone'=>$phone, 'mail'=>$mail,'createtime' => date('Y-m-d H:i:s', time()));
        $result = $this->db->insert('player_buglist', $value);
        if (true == $result) {
            //得到当前插入数据的id
            $curId = $this->db->insert_id();
            //echo " curId:$curId ";
            //cp_tips("success");
        } else {
            cp_tips("error");
        }
        /*
          $sql = "insert into player_buglist(platform,channel,accountid,roleid,question,createtime) "
          ."values('$platfrom','$channel',$account,$roleid,'$question','".date('Y-m-d h:i:s',time())."')";
          //echo " sql:$sql ";

          if( true == $this->db->query($sql))
          {
          //得到当前插入数据的id
          $curId = $this->db->insert_id();
          //echo " curId:$curId ";
          }else
          {
          cp_tips("error");
          }
         */

        $sql = "select * from player_buglist where platform = '$platfrom' "
                . "and channel = '$channel' and accountid =$account and roleid = $roleid  order by createtime desc";

        //echo " sql:$sql ";	
        $this->db->query($sql);
        while ($row = $this->db->get_array()) {

        	$row['question'] = base64_decode($row['question']);
        	
            $data[0][$row['id']]['question'] = $row['question'];
            $data[0][$row['id']]['answer'] = $row['answer'];
            $data[0][$row['id']]['createtime'] = $row['createtime'];
            $data[0][$row['id']]['isanswer'] = $row['isanswer'];

            if (1 == $row['isanswer']) { //已回答
                $data[1][$row['id']]['question'] = $row['question'];
                $data[1][$row['id']]['answer'] = $row['answer'];
                $data[1][$row['id']]['createtime'] = $row['createtime'];
                $data[1][$row['id']]['isanswer'] = $row['isanswer'];
            } else { //未回答
                $data[2][$row['id']]['question'] = $row['question'];
                $data[2][$row['id']]['answer'] = $row['answer'];
                $data[2][$row['id']]['createtime'] = $row['createtime'];
                $data[2][$row['id']]['isanswer'] = $row['isanswer'];
            }
        }

        //隐式的把数据传过去
        $this->view->assign("platform", $platfrom);
        $this->view->assign("channel", $channel);
        $this->view->assign("account", $account);
        $this->view->assign("area", $area);
        $this->view->assign("roleid", $roleid);
        $this->view->assign("mydata", $data);
        $this->view->display("buglist/playerbug/index");
    }

}
